<?php
/*
 * Created on Sep 13, 2010
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */
 
include_once("./configure/configure.php");		//--> global var
include_once("Smarty.class.php");				//--> out template
include_once("Common.php");
include_once("UploadFiles.php");				//--> UploadFiles
include_once("Strings.php");					//--> String utils
include_once("customerSession.inc.php");
include_once("Session.php");					//-- Session
include_once("orders/Cart.php");					//--> Cart
include_once("orders/CartProduct.php");				//--> Cart
include_once("orders/CartProductAttribute.php");	//--> Cart

$common 	= new Common();
$objStrings = new Strings();	
$objSession	= new Session(DB_TAG_SYSTEM, SESSION_TABLE_NAME);	//-- session
$objCart 	= new Cart();		//--> Cart

session_start();

$isLogin	= false;

if($objSession->exist()) {
	$isLogin	= true;
}

if(isset($_POST['id']))
	$id = $_POST['id'];
else
	$id = $_GET['id'];


$isYouth 		= 'no';
$finalPrice		= '0';
$promoDescript 	= '';

$sqlProducts	= "SELECT p.id, p.price, p.model, p.shippingTypeID, pd.name, pd.description, p.r_rating, p.r_reviewCount FROM products p, products_description pd WHERE p.status in ('normal', 'hot') AND p.id = pd.productID AND p.id = $id";

$product 		= $common->getRow(DB_TAG_PUBLIC, $sqlProducts);
if(count($product) == 0){
	header("Location: " . HOME_URL);
	
	exit;
}

include_once("includeCategory.php");	//--> include category
include_once("includeSpec.php");		//--> include spec

$specialSql		= "SELECT newPrice FROM specials WHERE productID = $id";
$newPrice		= $common->getColumn(DB_TAG_PUBLIC, $specialSql);
if(!empty($newPrice))
	$finalPrice = $newPrice;

$product['finalPrice'] = $finalPrice;


$sql			= "SELECT descript FROM promotion WHERE status = 'normal' AND productId = $id";
$promoDescrit	= $common->getColumn(DB_TAG_PUBLIC, $sql);
if(!empty($promoDescrit))
	$promoDescrit = $promoDescrit;
	
$product['promoDescrit'] = $promoDescrit;

	
$attributePrice	= array();
$attributes		= array();
$sortsValue		= array();

$sizeValuess 	= $common->getAllData(DB_TAG_PUBLIC, "SELECT pa.attributeID, a.name, a.parentid, pa.attributePrice, pa.pricePrefix FROM attributes a, products_attributes pa WHERE a.parentid != 33 AND a.id = pa.attributeID AND pa.productID = $id ORDER BY a.sorts");
foreach($sizeValuess as $arrRow){
	if(!empty($arrRow['parentid'])){
		if($arrRow['attributePrice'] > 0)
			$attributePrice[$arrRow['attributeID']] = $arrRow['attributePrice'];
		
		$attribute = $common->getRow(DB_TAG_PUBLIC, "SELECT name, sorts FROM attributes WHERE id = " . $arrRow['parentid']);
		
		if(array_key_exists($attribute['name'], $attributes)){
			$arrSub 	= $attributes[$attribute['name']];
			$arrData 	= $arrSub['attri'];
			
			$key = $arrRow['attributeID']; //attribute value
			
			if($arrRow['attributePrice'] > 0)
				$arrData[$key] = $arrRow['name'] . ' ($' . $arrRow['pricePrefix'] . ($arrRow['attributePrice'] / 100) . ')';
			else
				$arrData[$key] = $arrRow['name'];
			
			$arrSub['attri'] = $arrData;
			
			$attributes[$attribute['name']] = $arrSub;
		}else{
			$arrData 			= array();
			$arrOption 			= array();
			
			$arrSub 			= array();
			
			$key = $arrRow['attributeID']; //attribute value
			
			if($arrRow['attributePrice'] > 0){
				$arrData[$key] = $arrRow['name'] . ' ($' . $arrRow['pricePrefix'] . ($arrRow['attributePrice'] / 100) . ')';
			}else
				$arrData[$key] = $arrRow['name'];
				
			$arrSub['attri'] 	= $arrData;
			
			$attributes[$attribute['name']] = $arrSub;
			
			$sortsValue[] = $attribute['sorts'];
			
			
			
		}
		
		
		// for display youth size or adult
		if($attribute['name'] == 'Size'){
			$pos = strpos($arrRow['name'], 'Y');
			
			if($pos === false){
				continue;
			}else if($pos == 0){
				$isYouth = 'yes';
			}
		}
	}
}

array_multisort($sortsValue, $attributes);

$i = 0;
$firstImageID = 0;
$firstImageExt= '';


$sql = "SELECT u.id, u.extName, p.attributeID, p.isMain FROM upload_files u, product_images p WHERE u.id = p.imageID AND p.productID = $id AND u.type = " . UploadFiles::TYPE_IMAGE . ' ORDER BY p.isMain';

$arrAllImages = $common->getAllData(DB_TAG_PUBLIC, $sql);
$arrImages = array();
foreach($arrAllImages as $img){
	if(empty($firstImageExt)){
		$firstImageID		= $img['id'];
		$firstImageExt		= $img['extName'];
	}else{
		$arrImages[] = $img;
	}
	
}


$arrRelatedProduct = array();
$allRelatedProducts = $common->getAllData(DB_TAG_PUBLIC, "SELECT p.relatedProdID, pd.name FROM products_related p, products_description pd WHERE p.type = 1 AND p.relatedProdID = pd.productID AND p.productID = $id");

foreach($allRelatedProducts as $relatedRow){
	$sql 				= "SELECT u.id, u.extName, p.attributeID, p.isMain FROM upload_files u, product_images p WHERE u.id = p.imageID AND p.productID = " . $relatedRow['relatedProdID'] . " AND u.type = " . UploadFiles::TYPE_IMAGE . ' ORDER BY p.isMain limit 1';
	$arrRelatedImage 	= $common->getRow(DB_TAG_PUBLIC, $sql);
	
	if($arrRelatedImage['attributeID'] > 0){
		$sql 			= "SELECT name FROM attributes WHERE id = " . $arrRelatedImage['attributeID'];
		$attributeName 	= $common->getColumn(DB_TAG_PUBLIC, $sql);
	}else
		$attributeName 	= 'Other';
	
	$relatedRow['imageID'] 		= $arrRelatedImage['id'];
	$relatedRow['imageExtName'] = $arrRelatedImage['extName'];
	$relatedRow['imageName'] 	= $attributeName;
	
	$arrRelatedProduct[] = $relatedRow;
}

$sql = "UPDATE products_description SET r_viewed = r_viewed + 1 WHERE productid = $id";
$common->update(DB_TAG_PUBLIC, $sql);

if($_SESSION['cart'])
	$objCart = unserialize($_SESSION['cart']);
else
	$objCart = new Cart();	//--> Cart


// add product to cart
if(isset($_POST['addCart'])){
	$objCartProduct = new CartProduct();
	
	$objCartProduct->setID($id);
	$objCartProduct->setName($product['name']);
	$objCartProduct->setPrice($product['price']);
	
	if(isset($_POST['quantity']) && is_numeric($_POST['quantity']))
		$objCartProduct->setQuantity($_POST['quantity']);
	else
		$objCartProduct->setQuantity(1);
	
	if(!empty($finalPrice))
		$objCartProduct->setFinalPrice($finalPrice);
	else
		$objCartProduct->setFinalPrice($product['price']);

	$objCartProduct->setModel($product['model']);
	$objCartProduct->setImageFileName($firstImageID . '.' . $firstImageExt);
	$objCartProduct->setShippingTypeID($product['shippingTypeID']);
	
	if(isset($_POST['attribute']) && is_array($_POST['attribute'])){
		$arrAttribute = $_POST['attribute'];
		
		foreach($arrAttribute as $attributeid){
			$sqlattri = "SELECT parentid, name FROM attributes WHERE id = $attributeid";

			$arrAttri = $common->getRow(DB_TAG_PUBLIC, $sqlattri);
			
			$sqlattri = "SELECT name FROM attributes WHERE id = " . $arrAttri['parentid'];

			$arrAttriParent = $common->getRow(DB_TAG_PUBLIC, $sqlattri);
			
			$objCartProductAttribute 	= new CartProductAttribute();	//--> Cart
			
			$objCartProductAttribute->setID($attributeid);
			$objCartProductAttribute->setKey($arrAttriParent['name']);
			$objCartProductAttribute->setValue($arrAttri['name']);
			
			if(array_key_exists($attributeid, $attributePrice))
				$objCartProductAttribute->setPrice($attributePrice[$attributeid]);
			else
				$objCartProductAttribute->setPrice(0);

			$objCartProduct->setAttribute($objCartProductAttribute);
		}
	}

	$objCart->setStatus(Cart::STATUS_CHANGED);

	$objCart->setProduct($objCartProduct);

	$_SESSION['cart'] = serialize($objCart);
	
	
	header("Location: " . HOME_URL . '/viewCart.php');
	
	exit;
}



$sql 		= "*";
$where 		= "reviews WHERE product_id = $id";
$allReviews = $common->listCustom(DB_TAG_PUBLIC, $sql, $where , "", $page, 5);


/*----- out html -----*/
$smarty = new Smarty();	//-- out template
$smarty->template_dir 	= TEMPLATE_DIR;
$smarty->compile_dir  	= COMPILE_DIR;
$smarty->cache_dir  	= CACHE_DIR;

$smarty->force_compile 	= false;
$smarty->debugging 		= false;
$smarty->caching 		= CACHING;
$smarty->cache_lifetime = CACHE_LIFETIME;

$smarty->assign('HOME_URL', HOME_URL);
$smarty->assign('HOME_URL_HTTP', HOME_URL);

$smarty->assign('categorys', $categorys);
$smarty->assign('topCategory', $topCategory);
$smarty->assign('specProducts', $specProducts);

$smarty->assign('product', $product);

$smarty->assign('arrRelatedProduct', $arrRelatedProduct);

$smarty->assign('attributes', $attributes);
$smarty->assign('arrImages', $arrImages);

$smarty->assign('firstImageID', $firstImageID);
$smarty->assign('firstImageExt', $firstImageExt);

$smarty->assign('id', $id);
$smarty->assign('objCart', $objCart);
$smarty->assign('objStrings', $objStrings);

$smarty->assign('finalPrice', $finalPrice);

$smarty->assign('isLogin', $isLogin);

$smarty->assign('allReviews', $allReviews);


$smarty->display('productDetails.html');
?>
